﻿using AntdUI;
using System.Net.NetworkInformation;
using System.Text;

namespace DataCollection.Infrastructure
{
    public class Util
    {
        public static readonly List<MenuItems> MenuItems = new List<MenuItems>{
           new MenuItems { Text = "工程" , Tag = "ProjectManage", IconSvg="ProjectOutlined"},
           new MenuItems { Text = "设置", Tag = "SystemSet", IconSvg="SettingOutlined"},
           new MenuItems { Text = "模板", Tag = "TemplateManage", IconSvg="DatabaseOutlined"},
           new MenuItems { Text = "设备", Tag = "DeviceManage", IconSvg="NodeCollapseOutlined"},
           new MenuItems { Text = "界面", Tag = "FormManage", IconSvg="AppstoreOutlined"},
           new MenuItems { Text = "报告", Tag = "Report", IconSvg="ControlOutlined"},
           //new MenuItems { Text = "悬浮按钮", Tag = "FloatButton", IconSvg="ControlOutlined"},
           new MenuItems { Text = "历史", Tag = "History", IconSvg="HistoryOutlined"},
        };

        /// <summary>
        /// 秒->时:分:秒
        /// </summary>
        /// <param name="duration"></param>
        /// <returns></returns>
        static public string SecondToHMS(long duration)
        {
            TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));
            string str = "";
            if (ts.Hours > 0)
            {
                str = String.Format("{0:00}", ts.Hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes > 0)
            {
                str = "00:" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes == 0)
            {
                str = "00:00:" + String.Format("{0:00}", ts.Seconds);
            }
            return str;
        }

        /// <summary>十六进制字符串转byte
        /// 
        /// </summary>
        /// <param name="hs"></param>
        /// <returns></returns>
        public static byte[] HexStringToBytes(string hs)
        {
            string a = hs.Replace(" ", "");
            int bytelength = 0;
            if (a.Length % 2 == 0)
            {
                bytelength = a.Length / 2;
            }
            else
            {
                bytelength = a.Length / 2 + 1;
            }
            byte[] b = new byte[bytelength];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < bytelength; i++)
            {
                if (i == bytelength - 1)
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2), 16);
                }
                else
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
                }
            }
            //按照指定编码将字节数组变为字符串
            return b;
        }

        /// <summary>字符串转GB2312
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetHexFromChs(string s)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(s);
            StringBuilder Text = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                Text.Append(string.Format("{0:X}", bytes[i]));
                //Text.Append(" ");
            }

            string ggg = Text.ToString();
            string ggf = ggg.Substring(ggg.Length - 1, 1);
            if (ggf == " ")
            {
                ggg = ggg.Remove(ggg.Length - 1, 1);
            }
            return ggg;
        }

        static public string TodayWeek()
        {
            string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];

            return week;
        }

        /// <summary>
        /// 转时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        static public long Timestamp(DateTime time)
        {
            DateTimeOffset dateTimeOffset = new DateTimeOffset(time);
            return dateTimeOffset.ToUnixTimeSeconds();
        }

        static public DateTime ConvertToDateTime(long stamp, bool Millisecond = false)
        {
            string ID = TimeZoneInfo.Local.Id;
            DateTime start = new DateTime(1970, 1, 1) + TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
            DateTime startTime = TimeZoneInfo.ConvertTime(start, TimeZoneInfo.FindSystemTimeZoneById(ID));
            DateTime dt;
            if (Millisecond)
                dt = startTime.AddMilliseconds(stamp);
            else
                dt = startTime.AddSeconds(stamp);
            Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:fff"));
            return dt;
        }

        static public string IPAddress()
        {

            // 获取所有网络接口的信息
            var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var network in networkInterfaces)
            {
                if (network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                {
                    var properties = network.GetIPProperties();
                    foreach (var address in properties.UnicastAddresses)
                    {
                        if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            return address.Address.ToString();
                        }
                    }
                }
            }

            return "";
        }
    }

    public class MenuItems
    {
        public string IconSvg { get; set; } = null!;
        public string Text { get; set; } = string.Empty;
        public string Tag { get; set; } = null!;
    }
}

    
